package com.ace.scenep.day0114;

import java.io.*;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 简单文件处理工具类
 */
public class FileUtils {
    
    /**
     * 读取文本文件内容
     */
    public static String readFile(String filePath) throws IOException {
        StringBuilder content = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                content.append(line).append("\n");
            }
        }
        return content.toString();
    }
    
    /**
     * 按行读取文件
     */
    public static List<String> readLines(String filePath) throws IOException {
        List<String> lines = new ArrayList<>();
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                lines.add(line);
            }
        }
        return lines;
    }
    
    /**
     * 写入文本文件
     */
    public static void writeFile(String filePath, String content) throws IOException {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
            writer.write(content);
        }
    }
    
    /**
     * 复制文件
     */
    public static void copyFile(String sourcePath, String targetPath) throws IOException {
        try (InputStream in = new FileInputStream(sourcePath);
             OutputStream out = new FileOutputStream(targetPath)) {
            byte[] buffer = new byte[1024];
            int length;
            while ((length = in.read(buffer)) > 0) {
                out.write(buffer, 0, length);
            }
        }
    }
    
    /**
     * 移动文件
     */
    public static void moveFile(String sourcePath, String targetPath) throws IOException {
        Path source = Paths.get(sourcePath);
        Path target = Paths.get(targetPath);
        Files.move(source, target, StandardCopyOption.REPLACE_EXISTING);
    }
    
    /**
     * 创建目录
     */
    public static void createDirectory(String dirPath) {
        File dir = new File(dirPath);
        if (!dir.exists()) {
            dir.mkdirs();
        }
    }
    
    /**
     * 删除文件或目录
     */
    public static void delete(String path) throws IOException {
        File file = new File(path);
        if (file.isDirectory()) {
            // 如果是目录，先删除目录下的所有文件
            File[] files = file.listFiles();
            if (files != null) {
                for (File f : files) {
                    delete(f.getPath());
                }
            }
        }
        Files.delete(Paths.get(path));
    }
    
    /**
     * 获取文件信息
     */
    public static FileInfo getFileInfo(String filePath) {
        File file = new File(filePath);
        FileInfo info = new FileInfo();
        info.setName(file.getName());
        info.setPath(file.getAbsolutePath());
        info.setSize(file.length());
        info.setDirectory(file.isDirectory());
        info.setLastModified(file.lastModified());
        return info;
    }
    
    /**
     * 文件信息类
     */
    public static class FileInfo {
        private String name;
        private String path;
        private long size;
        private boolean isDirectory;
        private long lastModified;
        
        // getter和setter方法
        public String getName() { return name; }
        public void setName(String name) { this.name = name; }
        public String getPath() { return path; }
        public void setPath(String path) { this.path = path; }
        public long getSize() { return size; }
        public void setSize(long size) { this.size = size; }
        public boolean isDirectory() { return isDirectory; }
        public void setDirectory(boolean directory) { isDirectory = directory; }
        public long getLastModified() { return lastModified; }
        public void setLastModified(long lastModified) { this.lastModified = lastModified; }
    }
} 